package com.clouds.wms.pur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Purma;
import com.clouds.wms.bean.Purmc;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.PurmcService;
import com.clouds.wms.service.PurmdService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/purmc")
public class PurmcController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="purmcService")
	private PurmcService purmcService;
	@Resource(name="purmdService")
	private PurmdService purmdService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@RequestMapping(value = "/purmcMain.xhtml")
	public String purmcMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGSH));
		return "pur/purmc";
	}
	@RequestMapping(value = "/purmcList.xhtml")
	public String purmcList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mcaa = RequestHelper.getString(request, "mcaa");
		String mcab = RequestHelper.getString(request, "mcab");
		String mcac = RequestHelper.getString(request, "mcac");
		String startMcad = RequestHelper.getString(request, "startMcad");
		String endMcad = RequestHelper.getString(request, "endMcad");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = purmcService.getPurmcList(mcaa, mcab, mcac, startMcad, endMcad, pageNumber, pageSize);
		List<Purmc> list = (List<Purmc>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单头
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addPurmc.xhtml", method = RequestMethod.POST)
	public String addPurma(HttpServletRequest request, HttpServletResponse response, Model model, Purmc purmc) throws SysException, IOException {
		String mcaa = purmc.getMcaa();
		String mcab = purmc.getMcab();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Purmc purmctmp = purmcService.getPurmc(mcaa,mcab);
		if(purmctmp != null && purmctmp.getMcaa() != null && !"".equals(purmctmp.getMcaa())) {
			out.print("该收货单的编号已经存在，请重新录入!");
			return null;
		}
		purmc.setMcad(Function.getDateString(new Date()));
		purmcService.savePurmc(purmc);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + mcab + "]的收货单!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
}
